package com.qijian.tool.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qijian.common.annotation.Excel;
import com.qijian.common.core.domain.BaseEntity;
import com.qijian.common.utils.file.FileUtils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * 本地存储对象 tool_storage
 *
 * @author qijian
 * @date 2021-09-06
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString
@TableName("tool_storage")
@Accessors(chain = true)
public class Storage extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * ID
     */
    @TableId
    @JsonSerialize(using = ToStringSerializer.class)
    @ApiModelProperty(value = "ID")
    private Long storageId;

    /**
     * 来源表
     */
    @Excel(name = "来源表")
    @ApiModelProperty(value = "来源表")
    private String classTable;

    /**
     * 来源表ID
     */
    @Excel(name = "来源表ID")
    @ApiModelProperty(value = "来源表ID")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long classId;

    private Integer classify;

    /**
     * 文件真实的名称
     */
    @Excel(name = "文件真实的名称")
    @ApiModelProperty(value = "文件真实的名称")
    private String realName;

    /**
     * 文件名
     */
    @Excel(name = "文件名")
    @ApiModelProperty(value = "文件名")
    private String name;

    /**
     * 后缀
     */
    @Excel(name = "后缀")
    @ApiModelProperty(value = "后缀")
    private String suffix;

    /**
     * 路径
     */
    @Excel(name = "路径")
    @ApiModelProperty(value = "路径")
    private String url;

    /**
     * 类型
     */
    @Excel(name = "类型")
    @ApiModelProperty(value = "类型")
    private String type;

    /**
     * 大小
     */
    @Excel(name = "大小")
    @ApiModelProperty(value = "大小")
    private String size;

    /**
     * 存储类型
     */
    @Excel(name = "存储类型")
    @ApiModelProperty(value = "存储类型")
    private StorageType storageType;

    /**
     * 备注
     */
    @Excel(name = "备注")
    @ApiModelProperty(value = "备注")
    private String description;

    @TableField(exist = false)
    private String code;

    private Long fid;

    public enum StorageType {
        /**
         * 本地
         */
        LOCAL,
        /**
         * 阿里云
         */
        ALIOSS
    }

    public static Storage instance(Long classId, String classTable, String fileName, String url, String size) {
        Storage storage = new Storage();
        storage.setClassTable(classTable);
        storage.setClassId(classId);
        storage.setName(FileUtils.getFileNameNoEx(fileName));
        storage.setStorageType(Storage.StorageType.ALIOSS);
        storage.setRealName(fileName);
        storage.setSuffix(FileUtils.getExtensionName(fileName));
        storage.setUrl(url);
        storage.setType(FileUtils.getFileType(storage.getSuffix()));
        storage.setSize(size);
        return storage;
    }
}
